let jwt = require('jsonwebtoken')
let postList = async ctx => {
    let { username, password } = ctx.request.body;
    let token = jwt.sign({ username, password }, ctx.key, {
        expiresIn: '30d'
    })
    let sql = `select * from users where username='${username}'`
    let res = await ctx.db.execute(sql)
    // console.log(res);
    if (res.length === 1) {
        let sql1 = `select * from users where password=${password}`
        let res1 = await ctx.db.execute(sql1)
        // console.log(sql1);
        if (res1.length === 1) {
            ctx.body = { code: 200, msg: "登陆成功", token: token }
        } else {
            ctx.body = { code: 401, msg: "密码错误" }
        }
    } else {
        ctx.body = { code: 400, msg: "用户名错误" }
    }
}


let xiugai = async ctx => {
    // console.log(ctx.request.body);
    let { value, id } = ctx.request.body
    ctx.db.update('bokeuser', { password:value }, { userid: id }).then(res => {
        // console.log(res);
        if (res.protocol41) {
            console.log('更新成功');
        }
    });
}






let bokeDenglu = async ctx => {
    // console.log(ctx.request, '11111111111');
    let { username, password } = ctx.request.body;
    // console.log(ctx.request.body, '22222222222');
    let token = jwt.sign({ username, password }, ctx.key, {
        expiresIn: '30d'
    })
    let sql = `select * from bokeUser where name='${username}'`
    // console.log(sql);
    let res = await ctx.db.execute(sql)
    // console.log(res);
    // console.log(res);
    if (res.length === 1) {
        // let sql1 = `select * from bokeUser where password='${password}'`
        // let res1 = await ctx.db.execute(sql1)
        // console.log(sql1);
        if (res[0].password === password) {
            // console.log('111');
            ctx.body = { code: 200, msg: "登陆成功", data: res, token: token }
        } else {
            ctx.body = { code: 401, msg: "密码错误" }
        }
    } else {
        ctx.body = { code: 400, msg: "用户名错误" }
    }
}



let bokeZhuce = async ctx => {
    console.log(ctx.request.body);
    let { username, password, newName } = ctx.request.body
    let res = ctx.db.insert('bokeuser', { name: username, password: password, newName: newName })
    ctx.body = { code: 200, msg: res }
}



let bokeFabu = async ctx => {
    console.log(ctx.request.body);
    let { name, title, text,type,img,visitor,widthImg,xiangqing } = ctx.request.body
    let res =  ctx.db.insert('texts', { name: name, title: title, type: type, img: img, visitor: visitor, widthImg: widthImg, xiangqing: xiangqing})
    ctx.body = { code: 200, msg: res }
}















let addList = async ctx => {
    // console.log(ctx.request.body);
    let res = await ctx.db.insert('dataList', ctx.request.body)
    console.log(res);
}

let gaiHuiYuan = async ctx => {
    console.log(ctx.request.body);

    // res[0].name = ctx.request.body.name
    // res[0].zhekou = ctx.request.body.zhekou
    // res[0].tel = ctx.request.body.tel
    // res[0].type = ctx.request.body.type
    //改
    ctx.db.update('huiyuan', { name: ctx.request.body.name, zhekou: ctx.request.body.zhekou, tel: ctx.request.body.tel, type: ctx.request.body.type, }, { id: ctx.request.body.id }).then(res => {

        if (res.protocol41) {
            console.log('更新成功');
        }
    });
    ctx.body = await ctx.db.execute(`select * from huiyuan`)
}
let shanchuhuiyuan = async ctx => {
    // console.log(ctx.request.body);
    ctx.db.deleteBy('huiyuan', { id: ctx.request.body.id }).then(res => {
        // console.log(res);
        if (res.protocol41) {
            // console.log('删除成功')
        }

    });
    // let a = await ctx.db.execute(`select * from huiyuan`)
    // console.log(a);
    ctx.body = await ctx.db.execute(`select * from huiyuan`)
}

let addHuiyuan = async ctx => {
    ctx.db.insert('huiyuan', ctx.request.body).then(res => {
        console.log(res);
    });
    ctx.body = await ctx.db.execute(`select * from huiyuan`)
}


let addQuyu = async ctx => {
    ctx.db.insert('quyu2', ctx.request.body).then(res => {
        console.log(res);
    });
    ctx.body = await ctx.db.execute(`select * from quyu2`)
}
let userInfo = async ctx => {
    let { username, password } = ctx.request.body
    console.log(ctx.request.body);
    let sql = `select * from zt_user`;
    console.log(sql);
    sql += ` where name='${username}' AND password='${password}'`
    console.log(sql);
    let res = await ctx.db.execute(sql)
    console.log(res);
    console.log(res);
    if (res.length === 0) {
        ctx.body = {
            code: -1,
            msg: '登录失败，用户名或密码错误'
        }
    } else {
        let token = jwt.sign({ username, password }, ctx.key, {
            expiresIn: '30d'
        })
        ctx.body = {
            code: 1,
            token,
            name: res[0].name,
            avtor: res[0].avtor,
            router_id: res[0].router_id,
            suer_id: res[0].user_id
        }
    }
}
module.exports = {
    postList, addList, bokeFabu,gaiHuiYuan, shanchuhuiyuan, addHuiyuan, addQuyu, userInfo, bokeDenglu, bokeZhuce, xiugai
}